﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;

namespace 数字孪生Demo.Model3D
{
    public class ChimneyModel3D : VisualObject3D
    {
        public GeometryModel3D whiteBands = new GeometryModel3D();
        public GeometryModel3D redBands = new GeometryModel3D();

        public ChimneyModel3D()
        {
            whiteBands.Material = MaterialHelper.CreateMaterial(Colors.White,0.5);
            redBands.Material = MaterialHelper.CreateMaterial(Colors.Red, 0.5);

            InitModel();

            Model3DGroup group = new Model3DGroup();
            group.Children.Add(whiteBands);
            group.Children.Add(redBands);
            this.Visual3DModel = group;
        }

        #region 依赖属性
        public double Height
        {
            get { return (double)GetValue(HeightProperty); }
            set { SetValue(HeightProperty, value); }
        }

        public static readonly DependencyProperty HeightProperty =
            DependencyProperty.Register("Height", typeof(double), typeof(ChimneyModel3D), new PropertyMetadata(80.0, OnPropertyChanged));


        public double BaseDiameter
        {
            get { return (double)GetValue(BaseDiameterProperty); }
            set { SetValue(BaseDiameterProperty, value); }
        }

        public static readonly DependencyProperty BaseDiameterProperty =
            DependencyProperty.Register("BaseDiameter", typeof(double), typeof(ChimneyModel3D), new PropertyMetadata(6.0, OnPropertyChanged));


        public double TopDiameter
        {
            get { return (double)GetValue(TopDiameterProperty); }
            set { SetValue(TopDiameterProperty, value); }
        }

        public static readonly DependencyProperty TopDiameterProperty =
            DependencyProperty.Register("TopDiameter", typeof(double), typeof(ChimneyModel3D), new PropertyMetadata(3.0, OnPropertyChanged));


        public int BandCount
        {
            get { return (int)GetValue(BandCountProperty); }
            set { SetValue(BandCountProperty, value); }
        }

        public static readonly DependencyProperty BandCountProperty =
            DependencyProperty.Register("BandCount", typeof(int), typeof(ChimneyModel3D), new PropertyMetadata(8));

        #endregion

        #region 依赖方法
        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as ChimneyModel3D)?.InitModel();
        }


        #endregion

        private void InitModel()
        {
            MeshBuilder whiteBuilder = new MeshBuilder(false, false);
            MeshBuilder redBuilder = new MeshBuilder(false, false);
            double bandHeight = Height / BandCount;
            for (int i = 0; i < BandCount; i++)
            {
                double p0 = (double)i / BandCount;
                double p1 = (double)(i + 1) / BandCount;
                double y0 = Height * p0;
                double y1 = Height * p1;
                double r0 = BaseDiameter * (1 - p0) + TopDiameter * p0;
                double r1 = BaseDiameter * (1 - p1) + TopDiameter * p1;

                MeshBuilder builder = i % 2 == 0 ? whiteBuilder : redBuilder;
                builder.AddCone(new Point3D(0, 0, y0), new Vector3D(0, 0, 1), r0, r1, bandHeight, i == 0, i == BandCount - 1, 40);
            }

            whiteBands.Geometry = whiteBuilder.ToMesh(true);
            redBands.Geometry = redBuilder.ToMesh(true);
        }

        public override void OnSelected(bool isSelected)
        {
            double opacity = isSelected ? 1.0 : 0.5;
            whiteBands.Material = MaterialHelper.CreateMaterial(Colors.White, opacity);
            redBands.Material = MaterialHelper.CreateMaterial(Colors.Red, opacity);
        }
    }
}
